package com.youdianda.controller.user;


import com.youdianda.result.Result;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

//@RestController
//@RequestMapping("/common")
@Api(tags = "通用接口")
public class CommonFileController {

    @PostMapping("/upload")
    public Result upload(MultipartFile file, String type) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String[] split = originalFilename.split("\\.");
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String s = UUID.randomUUID() + "." + split[1];
        FileOutputStream stream = new FileOutputStream("C:\\Users\\86132\\Desktop\\nginx-front\\html\\dist\\apiupload\\images"+s);
        InputStream inputStream = file.getInputStream();
        byte[] bytes = new byte[1024];
        int lehsen;
        while ((lehsen = inputStream.read(bytes)) != -1) {
            stream.write(bytes, 0, lehsen);
        }
        stream.close();
        inputStream.close();
        Map map = new HashMap();

        map.put("savePath", "apiupload\\images\\20181015"+s);
        map.put("mime",substring);
        map.put("name",originalFilename);
        map.put("size",file.getSize());

        return Result.success(map);
    }
    }
